/*
===============================================================================
 Name        : main.c
 Author      : 
 Version     :
 Copyright   : Copyright (C) 
 Description : main definition
===============================================================================
*/

#ifdef __USE_CMSIS
#include "LPC17xx.h"
#endif
/*****************************************************************************
 *   History
 *   2010.07.01  ver 1.01    Added support for UART3, tested on LPCXpresso 1700
 *   2009.05.27  ver 1.00    Prelimnary version, first Release
 *
******************************************************************************/
#include "type.h"
#include "uart.h"
#include <string.h>
#include <stdlib.h>

extern volatile uint32_t UART3Count;
extern volatile uint8_t UART3Buffer[BUFSIZE];
extern volatile uint8_t comando_ready;
extern volatile uint8_t indice;
extern volatile uint8_t debugCmd;
volatile uint8_t flag_test=0;
volatile uint8_t debug=0;

void procesar_comandos(uint8_t d);
void process_debugcmd(char color);
void rgb_set(uint8_t a, char color);
void rgb_init(void);
void rgb_test(void);
void rgb_setAll(uint8_t a);
static void Delay (uint32_t dlyTicks);
volatile uint32_t msTicks;
uint8_t i=0;
uint8_t j=0;
volatile uint8_t k=0;
volatile uint8_t color = 'R';
volatile uint8_t cuenta=0;
volatile uint8_t fi=0;

volatile uint8_t demora = 0;


/*****************************************************************************
**   Main Function  main()
This program has been test on LPCXpresso 1700.
*****************************************************************************/
int main (void)
{
	const char* welcomeMsg = "\r\n Lampara RGB Cob Led:\r\n";
	const char* version = "Version 0.1\r\n";
	const char* dev = "Modo Debug ON \r\n";
	const char* noValido = "Comando NO Valido\r\n";


	uint8_t cmd;

	if (SysTick_Config(SystemCoreClock / 1000)) /* Setup SysTick Timer for 10 msec interrupts */
	{
	while (1); /* Capture error */
	}


		LPC_PINCON->PINSEL1 &= (~(3 << 12));
	    //Configuracion LED RGB
	    //Configuracion Como salida
		LPC_GPIO2->FIODIR |= (1 << 5);
		LPC_GPIO2->FIODIR |= (1 << 4);
		LPC_GPIO2->FIODIR |= (1 << 3);



	UARTInit(3, 9600);	/* baud rate setting */
	UARTSend(3, (uint8_t *)welcomeMsg , strlen(welcomeMsg) );

/*	LPC_GPIO2->FIOSET=(1<<3);
	Delay(500);
	LPC_GPIO2->FIOCLR=(1<<3);
*/
	rgb_init();

	/* Loop forever */
	while (1)
	{

		if(flag_test == 1){  //MODO PRUEBA LED
				rgb_test();
				   //UARTSend(3,"FLAG1",5);
			}

		if(UART3Count != 0 && debugCmd ==1 && debug == 0){  //Recibi Char pero no estoy modo DEV
				debugCmd =0;

				LPC_UART3->IER = IER_THRE | IER_RLS;
				UARTSend_Byte(3,UART3Buffer[UART3Count-1]);
				UARTSend_Byte(3,'\r');
				UARTSend_Byte(3,'\n');
				LPC_UART3->IER = IER_THRE | IER_RLS | IER_RBR;

				switch(UART3Buffer[indice-1]){
				case'?':
					UARTSend(3,(uint8_t *)version,strlen(version));
					UART3Count =0;
					break;
				case'd':
					UARTSend(3,(uint8_t *)dev,strlen(dev));
					debug = 1;
					UART3Count =0;
					break;
				default:
					UARTSend(3,(uint8_t *)noValido,strlen(noValido));
					UART3Count =0;
					break;
				}

		}else if(debug == 1 && UART3Count != 0 && debugCmd ==1){ //Recibi Char y estoy MODO DEV
			//Estos comandos son DEV, se habilitan solo cuando envio la .d]
				debugCmd =0;

				LPC_UART3->IER = IER_THRE | IER_RLS;
				UARTSend_Byte(3,UART3Buffer[UART3Count-1]);
				UARTSend_Byte(3,'\r');
				UARTSend_Byte(3,'\n');
				LPC_UART3->IER = IER_THRE | IER_RLS | IER_RBR;

				switch(UART3Buffer[indice-1]){
				case'?':
					UARTSend(3,(uint8_t *)version,strlen(version));
					UART3Count =0;
					break;
				case'Z': //Reseteo LED RGB
					//debug = 0;
					//UARTSend(3,"Mode Dev OFF\r",14);
					rgb_set(0,'R');
					rgb_set(0,'G');
					rgb_set(0,'B');
					UART3Count =0;
					break;
				case'R':
					indice--;
					process_debugcmd('R');

					UART3Count =0;
					break;
				case'G':
					indice--;
					process_debugcmd('G');
					UART3Count =0;
					break;
				case'B':
					indice--;
					process_debugcmd('B');
					UART3Count =0;
					break;

				case'C':				//Color BLANCO
					rgb_set(255,'R');
					rgb_set(255,'G');
					rgb_set(255,'B');
					break;
				case'T':                  //Led RGB TEST
				flag_test = 1;
				color = 'R';
				UART3Count = 0;
				break;

				default:
				UARTSend(3,(uint8_t *)noValido,strlen(noValido));
				UART3Count =0;
				break;

				}
				UART3Count =0;


		}else if ( UART3Count != 0 && comando_ready == 1)  //COMUNICACION por PROTOCOLO con PC-Bluetooth
		{
			//Recibi Comando
			// uint8_t  UART3Buffer[UART3Count]
			comando_ready = 0;
			LPC_UART3->IER = IER_THRE | IER_RLS;
			//UARTSend(3,(uint8_t *)greenStatus , strlen(greenStatus) );
			UARTSend_Byte(3,UART3Buffer[indice-1]);
			LPC_UART3->IER = IER_THRE | IER_RLS | IER_RBR;
			UART3Count =0;

			cmd = UART3Buffer[indice-1];
			procesar_comandos(cmd);

			}

}
}

/*-----------------------------------------------------------------------------
 * RGB TEST FUNCION
 *--------------------------------------------------------------------------*/
void rgb_test(void){

switch(color){

	case 'R':

			rgb_set(k,'R');
			if(demora == 1){
			k++;
			demora = 0;
			}

			if( k >= 254){
			//flag_test =0;
			k = 0;
			color = 'G';
			}
			break;


	case 'G':
		//UARTSend(3,"VERDE",5);
		rgb_set(k,'G');
					if(demora == 1){
					k++;
					demora =0;
					}

					if( k >= 254){
					//flag_test =0;
					k = 0;
					color = 'B';
					}
					break;

	case 'B':
		rgb_set(k,'B');
				if(demora == 1){
					k++;
					demora = 0;
					}

				if( k >= 254){
							//flag_test =0;
					k = 255;
					color = 'T';
					}
					break;
	case 'T':				//Terminado
		rgb_setAll(k);
		if(demora == 1){
			demora =0;
			k--;
			}
			if( k == 0 ){
				rgb_setAll(0);
				//flag_test =0;
				flag_test = 0;
				break;
			}
}

}

/*-----------------------------------------------------------------------------
  SysTick_Handler
*----------------------------------------------------------------------------*/
void SysTick_Handler (void)  {
  msTicks++;                                    // increment Delay()-counter
  // demora=1;
  //UARTSend_Byte(3,'.');

  cuenta++;
  if(cuenta == 10){ //FLAG DEMORA MARCA EVENTUALIDADES CADA 10ms
	  demora = 1;
	  cuenta = 0;
  }
}

/*-------------------------------------------------------------------------------
  delays number of tick Systicks (happens every 1 ms)
*------------------------------------------------------------------------------*/
__INLINE static void Delay (uint32_t dlyTicks)  {
  uint32_t curTicks;

  curTicks = msTicks;
  while ((msTicks - curTicks) < dlyTicks);
}


/*****************************************************************************
**                            End Of File
*****************************************************************************/


